package com.sgb.kjwl.view.ui.fragment.packers;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.PackersActivityTeamHallBinding;
import com.sgb.kjwl.view.ui.adapter.YoungAdapterOnItemClick;
import com.sgb.kjwl.view.ui.adapter.packers.TeamHallAdapter;
import com.sgb.link.router.PathLogin;
import com.sgb.link.router.PathTeam;
import com.sgb.packers.view.activity.AddProjectActivity;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.adapter.CommPagerAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : younghow
 * @date : 2021/12/6 9:57
 * description:
 */
public class TeamHallFragment extends AppLazyFragment {

    private PackersActivityTeamHallBinding mBinding;

    public static TeamHallFragment newInstance() {
        return new TeamHallFragment();
    }

    public static TeamHallFragment newInstance(String workKindOne,String workKindTwo,String workKindThree,String workName,String publishTime) {
        TeamHallFragment teamHallFragment = new TeamHallFragment();
        Bundle bundle = new Bundle();
        bundle.putString("workKindOne",workKindOne);
        bundle.putString("workKindTwo",workKindTwo);
        bundle.putString("workKindThree",workKindThree);
        bundle.putString("workName",workName);
        bundle.putString("publishTime",publishTime);
        teamHallFragment.setArguments(bundle);
        return teamHallFragment;
    }

    public static TeamHallFragment newInstance(int childPosition) {
        TeamHallFragment teamHallFragment = new TeamHallFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("childPosition",childPosition);
        teamHallFragment.setArguments(bundle);
        return teamHallFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.packers_activity_team_hall, container, false);
        return mBinding.getRoot();
    }

    @Override
    protected void onLazyLoad() {

        String[] title = {"施工队", "班组工队", "劳务公司", "记工记账"};
        List<String> list = new ArrayList<>(Arrays.asList(title));

        LinearLayoutManager linearLayout = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        TeamHallAdapter teamHallAdapter = new TeamHallAdapter(getContext(), list);
        teamHallAdapter.setItemSelect(true);
        teamHallAdapter.setMostNumber(2);
        teamHallAdapter.setDefItem(0);
        mBinding.tabLayout.setLayoutManager(linearLayout);
        mBinding.tabLayout.setAdapter(teamHallAdapter);
        teamHallAdapter.setOnItemClick(new YoungAdapterOnItemClick<String>() {
            @Override
            public void onItemClick(String s, int position) {
                if (position == 3){
                    if (!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))){
                        ActivityJump.startActivity(PathTeam.AddProjectActivity);
                    }else {
                        ActivityJump.startActivity(PathLogin.VerificationCodeLoginActivity);
                    }
                    return;
                }

                mBinding.vpTeamHall.setCurrentItem(position);
            }
        });

        ArrayList<Fragment> list1 = new ArrayList<>();
        String workKindOne;
        if (getArguments() != null) {
            workKindOne = getArguments().getString("workKindOne");
            String workKindTwo = getArguments().getString("workKindTwo");
            String workKindThree = getArguments().getString("workKindThree");
            String workName = getArguments().getString("workName");
            String publishTime = getArguments().getString("publishTime");
            list1.add(WorkerTeamFragment.newInstance(0, workKindTwo, workKindOne, workKindThree, workName, publishTime));
        }else {
            list1.add(WorkerTeamFragment.newInstance(0, "", "", "", "", ""));
        }

        list1.add(WorkerTeamFragment.newInstance(1, "", "", "", "", ""));
        list1.add(LaborServiceFragment.newInstance(getContext()));
        CommPagerAdapter pagerAdapter = new CommPagerAdapter(getChildFragmentManager(), list1, title);
        mBinding.vpTeamHall.setAdapter(pagerAdapter);
        mBinding.vpTeamHall.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                teamHallAdapter.setDefItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        if (getArguments() != null) {
            int childPosition = getArguments().getInt("childPosition");
            if (childPosition != 0){
                teamHallAdapter.setDefItem(childPosition);
                mBinding.vpTeamHall.setCurrentItem(childPosition);
            }
        }
    }
}
